using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
   public class tbDattourDAL
    {
        private SqlConnection cn = null;
        public tbDattourDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbDattour dt)
        {
            SqlCommand cm = new SqlCommand("sp_dattour_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matour", dt.Matour);
            cm.Parameters.AddWithValue("@mahinhthuctt", dt.Mahinhthuctt);
            cm.Parameters.AddWithValue("@makh", dt.Makh);
            cm.Parameters.AddWithValue("@songuoilon", dt.Songuoilon);
            cm.Parameters.AddWithValue("@songuoinho", dt.Songuoinho);
            cm.Parameters.AddWithValue("@yeucaukhac", dt.Yeucaukhac);
            cm.Parameters.AddWithValue("@ngaydattour", dt.Ngaydattour);
            cm.Parameters.AddWithValue("@ngaykhoihanh", dt.Ngaykhoihanh);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDattour dt)
        {
            SqlCommand cm = new SqlCommand("sp_dattour_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madattour", dt.Madattour);
            cm.Parameters.AddWithValue("@matour", dt.Matour);
            cm.Parameters.AddWithValue("@mahinhthuctt", dt.Mahinhthuctt);
            cm.Parameters.AddWithValue("@makh", dt.Makh);
            cm.Parameters.AddWithValue("@songuoilon", dt.Songuoilon);
            cm.Parameters.AddWithValue("@songuoinho", dt.Songuoinho);
            cm.Parameters.AddWithValue("@yeucaukhac", dt.Yeucaukhac);
            cm.Parameters.AddWithValue("@ngaydattour", dt.Ngaydattour);
            cm.Parameters.AddWithValue("@ngaykhoihanh", dt.Ngaykhoihanh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_dattour_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madattour", iD);
            cm.ExecuteNonQuery();
        }
        public tbDattourCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_dattour_Get_2000", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDattourCollection dtColl = new tbDattourCollection();

            while (dr.Read()) 
            {
                tbDattour dt = new tbDattour();
                dt.Madattour = int.Parse(dr.GetValue(0).ToString());
                dt.Matour = int.Parse(dr.GetValue(1).ToString());
                dt.Mahinhthuctt= int.Parse(dr.GetValue(2).ToString());
                dt.Makh = int.Parse(dr.GetValue(3).ToString());
                dt.Songuoilon = int.Parse(dr.GetValue(4).ToString());
                dt.Songuoinho = int.Parse(dr.GetValue(5).ToString());
                dt.Yeucaukhac = dr.GetValue(6).ToString();
                dt.Ngaydattour = DateTime.Parse(dr.GetValue(7).ToString());
                dt.Ngaykhoihanh = DateTime.Parse(dr.GetValue(8).ToString());
                dtColl.Add(dt);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dtColl;
        }
    }
}
